/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.Incubator;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.IncubatorRepository;
import me.zhengjie.modules.biz.service.IncubatorService;
import me.zhengjie.modules.biz.service.dto.IncubatorDto;
import me.zhengjie.modules.biz.service.dto.IncubatorQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.IncubatorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-16
**/
@Service
@RequiredArgsConstructor
public class IncubatorServiceImpl implements IncubatorService {

    private final IncubatorRepository incubatorRepository;
    private final IncubatorMapper incubatorMapper;

    @Override
    public Map<String,Object> queryAll(IncubatorQueryCriteria criteria, Pageable pageable){
        Page<Incubator> page = incubatorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(incubatorMapper::toDto));
    }

    @Override
    public List<IncubatorDto> queryAll(IncubatorQueryCriteria criteria){
        return incubatorMapper.toDto(incubatorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public IncubatorDto findById(Integer id) {
        Incubator incubator = incubatorRepository.findById(id).orElseGet(Incubator::new);
        ValidationUtil.isNull(incubator.getId(),"Incubator","id",id);
        return incubatorMapper.toDto(incubator);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IncubatorDto create(Incubator resources) {
        return incubatorMapper.toDto(incubatorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Incubator resources) {
        Incubator incubator = incubatorRepository.findById(resources.getId()).orElseGet(Incubator::new);
        ValidationUtil.isNull( incubator.getId(),"Incubator","id",resources.getId());
        incubator.copy(resources);
        incubatorRepository.save(incubator);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            incubatorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<IncubatorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (IncubatorDto incubator : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", incubator.getName());
            map.put("实验室", incubator.getLabId());
            map.put("负责人", incubator.getAdministrator());
            map.put("温度", incubator.getTemperature());
            map.put("温度阈值", incubator.getTemperatureThresholdValue());
            map.put("报警信息", incubator.getErrorMessage());
            map.put(" createdAt",  incubator.getCreatedAt());
            map.put(" updatedAt",  incubator.getUpdatedAt());
            map.put("是否删除", incubator.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}